package com.ch.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import vo.Student;

/**
 * @author chenhong
 * @version 1.0.0
 * @ClassName SingleReceiveController.java
 * @Description TODO
 * @createTime 2020年12月16日 21:27:00
 */

@Controller
@RequestMapping("single")
public class SingleReceiveController {


    /**
     * 逐个接收请求参数
     * 要求控制器方法的形参 和 请求中参数名必须一致
     * 框架提供类型转换机制，会把字符串自动转换成int double 等
     */
    @RequestMapping(value = "/receiveOne.do",method = RequestMethod.POST)
    public ModelAndView receiveOne(String name , Integer age){
        ModelAndView mv = new ModelAndView();
        mv.addObject("myName",name);
        mv.addObject("myAge",age);

        mv.setViewName("showSingle");
        return mv;

    }


   /**
    * 请求参数名和控制器形参名不一样
    * 此时可以使用注解 @RequestParam
    * value 请求中参数名称
    * required 默认为true表明 请求中必须包含此参数
    */
    @RequestMapping(value = "/receiveOne2.do",method = RequestMethod.POST)
    public ModelAndView receiveOne2(@RequestParam("myName") String name ,
                                    @RequestParam(value = "myAge",required = false) Integer age){
        ModelAndView mv = new ModelAndView();
        mv.addObject("myName",name);
        mv.addObject("myAge",age);

        mv.setViewName("showSingle");
        return mv;

    }

    /**
     *形参为java对象 ，其中对象的属性名 和 请求参数名相同
     */
    @RequestMapping(value = "/receiveObject.do",method = RequestMethod.POST)
    public ModelAndView receiveObject(Student student ){
        ModelAndView mv = new ModelAndView();
        mv.addObject("myName",student.getName());
        mv.addObject("myAge",student.getAge());

        mv.setViewName("showSingle");
        return mv;

    }
}
